package vy.org.app.page.subscription.magazine;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.vy.dal.dao.ViewVyPrintLabelDO;
import org.vy.dal.dao.VyAddressDO;
import org.vy.dal.dao.VyMagazineSubscriptionDO;

import vy.org.app.common.ActionCtx;
import vy.org.app.page.address.AddressHelper;
import vy.org.app.page.printlabel.group.GroupPrintLabelForm;

public class MagazineHelper {

	public static SimpleDateFormat _DATE_FMT = new SimpleDateFormat("MM/dd/yyyy");
	
	public static VyMagazineSubscriptionDO fillMagazineDO(MagazineForm form) throws Exception {
		VyMagazineSubscriptionDO dto = new VyMagazineSubscriptionDO();
		
		if(form.getAddress() != null)
			dto.setAddressId(form.getAddress().getAddressId());
		dto.setDeliveryMode(form.getDeliveryMode());
		dto.setInstruction(form.getInstruction());
		dto.setIsActive(Boolean.parseBoolean(form.getIsActive()));
		dto.setLastUpdatedBy(ActionCtx.ctx().getUser().getId());
		dto.setLastUpdatedOn(new Date());
		if(form.getPaidAmount() != null && !"".equals(form.getPaidAmount()))
			dto.setPaidAmount(Double.parseDouble(form.getPaidAmount()));
		if(form.getPatrikaId() !=null && !"".equals(form.getPatrikaId().trim()))
			dto.setPatrikaId(Integer.parseInt(form.getPatrikaId()));
		dto.setQuantity(Integer.parseInt(form.getQuantity()));
		if(form.getReceiptDate() != null && !"".equals(form.getReceiptDate()))
			dto.setReceiptDate(_DATE_FMT.parse(form.getReceiptDate()));
		dto.setReceiptNo(form.getReceiptNo());
		dto.setRefVyId(form.getRefVyId());
		dto.setStartDate(_DATE_FMT.parse(form.getStartDate()));
		dto.setEndDate(_DATE_FMT.parse(form.getEndDate()));
		
		if(form.getShippingAmount() != null && !"".equals(form.getShippingAmount()))
			dto.setShippingAmount(Double.parseDouble(form.getShippingAmount()));
		else
			dto.setShippingAmount(0.0);
		
		if(form.getSubscriptionId() != null && !"".equals(form.getSubscriptionId().trim()))
			dto.setSubscriptionId(form.getSubscriptionId());
		
		dto.setTotalAmount(Double.parseDouble(form.getTotalAmount()));
		dto.setVyId(form.getVyId());
		dto.setWeekOffsetIndex(Double.parseDouble(form.getWeekOffsetIndex()));
		
		return dto;
	}

	public static MagazineForm fillMagazineForm(VyMagazineSubscriptionDO dto, VyAddressDO addr) {
		MagazineForm form = new MagazineForm();
		
		if(addr != null)
			form.setAddress(AddressHelper.getAddress(addr));
		form.setDeliveryMode(dto.getDeliveryMode());
		form.setInstruction(dto.getInstruction());
		form.setIsActive(dto.getIsActive().toString());
		form.setLastUpdatedBy(dto.getLastUpdatedBy());
		form.setLastUpdatedOn(_DATE_FMT.format(dto.getLastUpdatedOn()));
		if(dto.getPaidAmount() != null && !"".equals(dto.getPaidAmount()))
			form.setPaidAmount(dto.getPaidAmount().toString());
		else
			form.setPaidAmount("0.0");
		form.setShippingAmount("0.0");
		form.setPatrikaId(dto.getPatrikaId().toString());
		form.setQuantity(dto.getQuantity().toString());
		if(dto.getReceiptDate() != null && !"".equals(dto.getReceiptDate()))
			form.setReceiptDate(_DATE_FMT.format(dto.getReceiptDate()));
		form.setReceiptNo(dto.getReceiptNo());
		form.setRefVyId(dto.getRefVyId());
		if(dto.getShippingAmount() != null && !"".equals(dto.getShippingAmount()))
			form.setShippingAmount(dto.getShippingAmount().toString());
		else
			form.setShippingAmount("0.0");
		
		form.setStartDate(_DATE_FMT.format(dto.getStartDate()));
		form.setEndDate(_DATE_FMT.format(dto.getEndDate()));
		int ppm = (dto.getEndDate().getYear() - dto.getStartDate().getYear()+1)*12;
		form.setPatrikaPeriod(ppm);
		
		form.setSubscriptionId(dto.getSubscriptionId());
		form.setTotalAmount(dto.getTotalAmount().toString());
		form.setVyId(dto.getVyId());
		form.setWeekOffsetIndex(dto.getWeekOffsetIndex().toString());
		
		return form;
	}
	
	public static GroupPrintLabelForm fillMagazineGroupForm(ViewVyPrintLabelDO dto, VyAddressDO addr) {
		GroupPrintLabelForm form = new GroupPrintLabelForm();
		
		if(addr != null)
			form.setAddress(AddressHelper.getAddress(addr));
		form.setDeliveryMode(dto.getDeliveryMode());
		form.setInstruction(dto.getInstruction());
		form.setIsActive(dto.getIsActive().toString());
		form.setLastUpdatedBy(dto.getLastUpdatedBy());
		form.setLastUpdatedOn(_DATE_FMT.format(dto.getLastUpdatedOn()));
		form.setFirstName(dto.getFirstName());
		form.setLastName(dto.getLastName());
		form.setMiddleName(dto.getMiddleName());
		form.setGroupId(dto.getPrintLabelGroupId());
		form.setLabelAddress(dto.getIsLabelAddress()? dto.getSubscriptionId():"");
		form.setRegionalLangName(dto.getRegionalLangName());
		form.setRefVyId(dto.getRefVyId());

		if(dto.getPaidAmount() != null && !"".equals(dto.getPaidAmount()))
			form.setPaidAmount(dto.getPaidAmount().toString());
		else
			form.setPaidAmount("0.0");
		form.setShippingAmount("0.0");
		form.setPatrikaId(dto.getPatrikaId().toString());
		form.setQuantity(dto.getQuantity().toString());
		if(dto.getReceiptDate() != null && !"".equals(dto.getReceiptDate()))
			form.setReceiptDate(_DATE_FMT.format(dto.getReceiptDate()));
		form.setReceiptNo(dto.getReceiptNo());
		form.setRefVyId(dto.getRefVyId());
		if(dto.getShippingAmount() != null && !"".equals(dto.getShippingAmount()))
			form.setShippingAmount(dto.getShippingAmount().toString());
		else
			form.setShippingAmount("0.0");
		
		form.setStartDate(_DATE_FMT.format(dto.getStartDate()));
		form.setEndDate(_DATE_FMT.format(dto.getEndDate()));
		int ppm = (dto.getEndDate().getYear() - dto.getStartDate().getYear()+1)*12;
		form.setPatrikaPeriod(ppm);
		
		form.setSubscriptionId(dto.getSubscriptionId());
		form.setTotalAmount(dto.getTotalAmount().toString());
		form.setVyId(dto.getVyId());
		form.setWeekOffsetIndex(dto.getWeekOffsetIndex().toString());
		
		return form;
	}
}
